<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 支付宝支付响应处理
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Payment_Respond_Alipay extends EGP_Payment_Respond_Abstract
{

    /**
     * 获取订单号
     *
     * @return string
     */
    public function getId()
    {
        return $this->_result['out_trade_no'];
    }

    /**
     * 获取支付交易号
     *
     * @return string
     */
    public function getPaymentNo()
    {
        return $this->_result['trade_no'];
    }

    /**
     * 获取数据签名
     *
     * @return string
     */
    public function getSign()
    {
        return $this->_result['sign'];
    }

    /**
     * 获取订单总金额
     *
     * @return float
     */
    public function getAmount()
    {
        return floatval($this->_result['total_fee']);
    }

    /**
     * 获取付款时间
     *
     * @return integer
     */
    public function getTime()
    {
        return strtotime($this->_result['notify_time']);
    }

    /**
     * 获取支付状态
     *
     * @return integer
     */
    public function getStatus()
    {
        $status = strtoupper($this->_result['trade_status']);
        switch ($status) {
            case 'WAIT_BUYER_PAY':
            case 'WAIT_SELLER_CONFIRM_TRADE':   //初始化
                return EGP_Payment::STAT_INITED;
            case 'WAIT_SYS_CONFIRM_PAY':        //等待付款
                return EGP_Payment::STAT_WAIT_PAY;
            case 'WAIT_SELLER_SEND_GOODS':      //已收款，等待发货
                return EGP_Payment::STAT_PAID;
            case 'WAIT_BUYER_CONFIRM_GOODS':    //已发货
                return EGP_Payment::STAT_SHIPPED;
            case 'WAIT_SYS_PAY_SELLER':         //已收货
                return EGP_Payment::STAT_RECEIVED;
            case 'TRADE_FINISHED':              //交易完成
                return EGP_Payment::STAT_SUCCEED;
            case 'TRADE_CLOSED':                //交易关闭(未完成)
            default:
                return EGP_Payment::STAT_FAILED;
        }
    }

    /**
     * 验证数字签名
     *
     * @throws EGP_Exception
     */
    protected function _verifySign()
    {
        $args = empty($_GET) ? $_POST : $_GET;
        ksort($args);

        $sign = '';
        foreach ($args as $key => $value) {
            if ($value != '' && $key != 'sign' && $key != 'sign_type') {
                $sign .= ($sign == '' ? '' : '&') . "$key=" . $value;
            }
        }
        $sign = md5($sign . $this->_keyCode);

        if ($sign != $this->getSign()) {
            throw new EGP_Exception('数字签名认证失败');
        }
    }

}
